const state = {
  isLoading: false,
  timer: 0,
  isRouterAlive: true
}

const mutations = {
  SET_LOADING_STATUS: (state, status) => {
    clearTimeout(state.timer)
    if (status) {
      state.timer = setTimeout(() => {
        state.isLoading = true
      }, 300)
    } else {
      state.isLoading = false
    }
  },
  useRouterAlive(state) {
    state.isRouterAlive = false
    setTimeout(() => {
      state.isRouterAlive = true
    }, 300)
  }
}

const actions = {
  setLoading({ commit }, status) {
    commit('SET_LOADING_STATUS', status)
  }
}

const getters = {
  isLoading(state) {
    return state.isLoading
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions,
  getters
}
